package com.zbkc.adapter;

import java.util.List;

import com.zbkc.bean.FileBean;
import com.zbkc.main.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class CheckboxAdapter extends BaseAdapter {

	private Context mContext;
	private List<FileBean> mPersonList;
	private int mResource;
	private LayoutInflater mInflater;
	private boolean isEdit;

	public CheckboxAdapter(Context context, List<FileBean> personList,
			int resource, boolean isEdit) {
		mContext = context;
		mPersonList = personList;
		mResource = resource;
		mInflater = LayoutInflater.from(mContext);
		this.isEdit = isEdit;
	}

	@Override
	public int getCount() {
		return mPersonList.size();
	}

	@Override
	public Object getItem(int position) {
		return mPersonList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(mResource, parent, false);
		}
		TextView tvUserName = (TextView) convertView
				.findViewById(R.id.tv_filename);
		final CheckBox ckbItem = (CheckBox) convertView
				.findViewById(R.id.checkbox);
		ImageView ivArrow = (ImageView) convertView.findViewById(R.id.iv_arrow);
		FileBean person = mPersonList.get(position);
		tvUserName.setText(person.getFilename());
		ckbItem.setChecked(person.isCheck());
		ckbItem.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mPersonList.get(position).setCheck(ckbItem.isChecked());// 保存checkbox状态至位置对应的列表对象Person中
			}
		});
		person = null;

		if (isEdit) {
			ivArrow.setVisibility(View.GONE);
			ckbItem.setVisibility(View.VISIBLE);
		} else {
			ivArrow.setVisibility(View.VISIBLE);
			ckbItem.setVisibility(View.GONE);
		}
		return convertView;
	}

}